package com.qiyun.asyncarrange;
//199


import java.util.concurrent.*;

public class CompletableFutureTest04 {
    public static ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
            200,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(100000),
            Executors.defaultThreadFactory(),//默认线程工厂
            new ThreadPoolExecutor.AbortPolicy()//拒绝策略
            //如果不想抛弃还要执行。可以使用new ThreadPoolExecutor.CallerRunsPolicy()
    );

    /**
     * 线程串行化
     *
     * @param args
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        /**
         * public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
         * public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)
         * public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
         *
         *
         * public CompletableFuture<Void> thenAccept(Consumer<? super T> action)
         * public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action)
         * public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor)
         *
         * public CompletableFuture<Void> thenRun(Runnable action)
         * public CompletableFuture<Void> thenRunAsync(Runnable action)
         * public CompletableFuture<Void> thenRunAsync(Runnable action,Executor executor)
         *
         * thenApply 方法：当一个线程依赖另一个线程时，获取上一个任务返回的结果，并返回当前任务的返回值。
         * thenAccept 方法：消费处理结果。接收任务的处理结果，并消费处理，无返回结果。
         * thenRun 方法：只要上面的任务执行完成，就开始执行 thenRun，只是处理完任务后，执行thenRun 的后续操作
         *
         * 带有 Async 默认是异步执行的。同之前。
         * 以上都要前置任务成功完成。
         *
         */


            /*

              1. thenRun：不能获取到上一步的执行结果，无返回值
                    .thenRunAsync(() -> {
                        System.out.println("线程" + Thread.currentThread().getId() + ",执行。。。");
                    })
             */
        /*CompletableFuture<Void> voidCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一线程" + Thread.currentThread().getId() + ",执行。。。");
            int i = 10 / 3;
            System.out.println("运行结果..." + i);
            return i;
        }, executor).thenRunAsync(() -> {
            System.out.println("任务二线程" + Thread.currentThread().getId() + ",执行。。。");
        },executor);*/




                 /*

              2. thenAccept：能获取到上一步的执行结果，但是无返回值
                    .thenAcceptAsync(() -> {
                        System.out.println("线程" + Thread.currentThread().getId() + ",执行。。。");
                    })
             */
             /*CompletableFuture.supplyAsync(() -> {
                System.out.println("任务一线程" + Thread.currentThread().getId() + ",执行。。。");
                int i = 10 / 3;
                System.out.println("运行结果..." + i);
                return i;
             }, executor).thenAcceptAsync(res->{
                System.out.println("任务一线程的结果"+res);
                System.out.println("任务二线程" + Thread.currentThread().getId() + ",执行。。。");
             },executor);*/




                  /*

              3. thenApply：能获取到上一步的执行结果，并且有返回值
                    .thenApplyAsync(() -> {
                        System.out.println("线程" + Thread.currentThread().getId() + ",执行。。。");
                    })
             */
            CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
                System.out.println("任务一线程" + Thread.currentThread().getId() + ",执行。。。");
                int i = 10 / 3;
                System.out.println("运行结果..." + i);
                return i;
            }, executor).thenApplyAsync(res -> {
                System.out.println("任务一线程的结果" + res);
                System.out.println("任务二线程" + Thread.currentThread().getId() + ",执行。。。");
                return res;
            }, executor);

            System.out.println("获取返回值" + future.get());


    }


}

